/**
 * Created by lxh on 2018/1/6.
 */

import React, {PureComponent} from 'react';
import {connect} from 'dva';
import {Link} from 'dva/router';
import {Form, Card,message, Row, Col, Input, Select, Table, Button,Radio,Modal} from 'antd';
import PageHeaderLayout from '../../../layouts/PageHeaderLayout';
import ShopManageTable from '../../../components/ShopManage';
import AddGoodsTable from '../../../components/ShopManage/AddGoods/AddGoodsTable';
import styles from './ShopManage.less';
import {routerRedux} from 'dva/router';
import ShopManage from "./ShopManage";
import {powerControl} from '../../../utils/powerControl';

const FormItem = Form.Item;
const {Option} = Select;
const AuthButton = powerControl(Button);

@connect(state => ({
    shopManage: state.shopManage,
}))
@Form.create()
export default class ShopAddGoods extends PureComponent {
    state={
        id:'',
        returnPath:''
    }
    componentDidMount(){
        if(!this.props.location.state){
            this.props.dispatch(routerRedux.push('distributorManage/shopManage'))
        }
        this.setState({
            id:this.props.location.state,
            returnPath:this.props.location.returnPath
        })
        this.props.dispatch({
            type:'shopManage/queryShopGoodById',
            payload:{id:this.props.location.state},
        })
    }
    handleDelete=(record)=>{
        this.props.dispatch({
            type:'shopManage/deleteShopGoodsById',
            payload: {
                id: this.state.id,
                goodsId: record
            },
            callback:(response)=>{
                    if(response.result===0){
                        message.success("移除成功")
                        this.props.dispatch({
                            type:'shopManage/queryShopGoodById',
                            payload:{id:this.state.id}
                        })
                    }else{
                        message.error("移除失败")
                    }
                },
        })
    }
    handleTableChange=(pagination)=>{
        const {dispatch} = this.props;
        const params = {
            currentPage: pagination.current,
            pageSize: pagination.pageSize,
        };
        dispatch({
            type: 'shopManage/queryShopGoodById',
            payload:params,
        });
    }
    render() {
        const {shopManage:{data,loading,GoodsData}}=this.props;
        console.log(GoodsData)
        const{id}=this.state;
        const {returnPath}=this.state;
        console.log("returnPath"+returnPath)
        return (
            <PageHeaderLayout title="商店管理">
                <Card bordered={false}>
                    <div className={styles.tableList}>
                        <div style={{margin:10,border:0,padding:0}}>
                            <Row gutter={{md: 8, lg: 24, xl: 48}}>
                                <Col md={2} sm={24}>
                                    <Button type="default"><Link to={{pathname:returnPath}}>返回</Link></Button>
                                </Col>
                                <Col md={2} sm={24}>
                                    <AuthButton  auth="goodsmanager.goodnew"  type="primary"><Link to={{pathname:'/distributorManage/shopSelectGoods',state:id,returnPath:returnPath}}>添加商品</Link></AuthButton >
                                </Col>
                            </Row>
                        </div>
                        <AddGoodsTable
                            loading={loading}
                            handleAdd={this.handleAdd}
                            handleDelete={this.handleDelete}
                            data={GoodsData}
                            handleTableChange={this.handleTableChange}
                            shopId={this.state.id}
                            returnPath={this.state.returnPath}
                        />
                        <div>

                        </div>
                    </div>
                </Card>
            </PageHeaderLayout>
        )
    }
}